package com.ysg.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class YTimeUtil {
    public static final String FORMAT_DATE = "yyyy-MM-dd";

    /**
     * 是否为今天
     */
    public static boolean isToday(String str) {
        if (YStringUtil.isEmpty(str)){
            return false;
        }
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_DATE);
        Date date;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            return false;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        int year1 = c1.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH) + 1;
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(new Date());
        int year2 = c2.get(Calendar.YEAR);
        int month2 = c2.get(Calendar.MONTH) + 1;
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        if (year1 == year2 && month1 == month2 && day1 == day2) {
            return true;
        }
        return false;
    }

    public static String getToday() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return year + "-" + (month > 9 ? month : ("0" + month)) + "-" + day;
    }

    /**
     * 过去，未来的今天
     * 过去：负数
     * 未来：正数
     */
    public static String getTodayByYear(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, year);
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = format.format(date);
        Log.e("去年的今日日期是：", formatDate);
        return formatDate;
    }

    /**
     * 过去，未来的今天
     * 过去：负数
     * 未来：正数
     */
    public static Date getTodayDateByYear(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, year);
        Date date = calendar.getTime();
        return date;
    }

}
